/**
 * 
 */
package vue.desktop;

import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JPanel;

import vue.popup.IJPopupMenu;
import vue.popup.JPopupPreprocessor;
import vue.session.IGPreprocessorBlock;
import controller.ISessionManager;

/**
 * Represent a preprocessor execution on the desktop.
 * 
 * @author GRONOWSKI Stephane svan001@aol.com
 * 
 *         igsat 2010
 * 
 */
public class IGDeskPreprocessor extends IGDeskItem implements MouseListener {

	private static final long	serialVersionUID		= 1L;
	public static final String	DEFAULT_DESCRIPTION		= "Execution";
	public static final String	UNKNOW					= "UNKNOW";
	public static final int		DESCRIPTION_FONT_SIZE	= 13;
	public static final int		TIME_MEMORY_FONT_SIZE	= 11;


	private IGPreprocessorBlock	preproc;
	private JLabel				description;
	private IJPopupMenu			rightMenu;

	// private JLabel status;
	private JLabel				time;
	private JLabel				totalTime;


	/**
	 * Create an {@link IGDeskPreprocessor}.
	 * 
	 * @param exec
	 *            the {@link IGPreprocessorBlock} associate
	 * @param manager
	 *            the session manager
	 */
	public IGDeskPreprocessor(IGPreprocessorBlock exec, ISessionManager manager) {
		preproc = exec;

		setLayout(new GridLayout(2, 1));

		// descritpion
		description = new JLabel(DEFAULT_DESCRIPTION, JLabel.CENTER);
		description.setFont(new Font(Font.MONOSPACED, Font.PLAIN, DESCRIPTION_FONT_SIZE));
		add(description);

		JPanel panel = new JPanel(new GridLayout(1, 2));
		panel.setOpaque(false);

		// status
		// status = new JLabel(UNKNOW, JLabel.CENTER);
		// status.setBorder(BorderFactory.createMatteBorder(1, 0, 0, 0, Color.BLACK));
		// status.setOpaque(false);
		// panel.add(status);

		// total time
		totalTime = new JLabel(UNKNOW, JLabel.CENTER);
		totalTime.setBorder(BorderFactory.createMatteBorder(1, 0, 0, 1, Color.BLACK));
		totalTime.setFont(new Font(Font.MONOSPACED, Font.PLAIN, TIME_MEMORY_FONT_SIZE));
		totalTime.setOpaque(false);
		panel.add(totalTime);

		// time
		time = new JLabel(UNKNOW, JLabel.CENTER);
		time.setBorder(BorderFactory.createMatteBorder(1, 0, 0, 0, Color.BLACK));
		time.setFont(new Font(Font.MONOSPACED, Font.PLAIN, TIME_MEMORY_FONT_SIZE));
		time.setOpaque(false);
		panel.add(time);

		add(panel);

		// pop up
		rightMenu = new JPopupPreprocessor(exec, manager);

		refresh();

		addMouseListener(this);
		setSize(getPreferredSize());

	}

	/**
	 * refresh the informations.
	 */
	public void refresh() {
		description.setText(preproc.getPreproc().getPreproc().getType().getDescription());
		// status.setText(preproc.getPreproc().getRes().getStatus().name());

		if (preproc.getPreproc().getRes() == null)
		{// pas de res
			time.setText(UNKNOW);
			totalTime.setText(UNKNOW);
		}
		else
		{// result dispo
			time.setText("" + preproc.getPreproc().getRes().getExecutionTime());
			totalTime.setText("" + preproc.getPreproc().getTotalExecutionTime());
		}

		rightMenu.refresh();
	}

	/**
	 * Return the {@link IGPreprocessorBlock} associate.
	 * 
	 * @return the {@link IGPreprocessorBlock} associate.
	 */
	public IGPreprocessorBlock getPreproc() {
		return preproc;
	}

	@Override
	public void mouseClicked(MouseEvent e) {
		preproc.notifySelected();
	}


	@Override
	public void mouseEntered(MouseEvent e) {
	}


	@Override
	public void mouseExited(MouseEvent e) {
	}


	@Override
	public void mousePressed(MouseEvent e) {
		if (e.isPopupTrigger())
			rightMenu.show(e.getComponent(), e.getX(), e.getY());
	}


	@Override
	public void mouseReleased(MouseEvent e) {
		if (e.isPopupTrigger())
			rightMenu.show(e.getComponent(), e.getX(), e.getY());
	}

	@Override
	public String toString() {
		return "IGDeskPreprocessor : " + preproc.getPreproc().getDescritpion();
	}
}
